﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CSharp.Libs.Koc;
using CSharp.Libs.Koc.ItemClasses;
using CSharp.Libs.WPF.UserControls;

namespace KocMarcher
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Kernel _KRNL = new Kernel();
        private KocKernel2ViewModel _KK2VM = null;

        public MainWindow()
        {
            InitializeComponent();

            kocKernel2Control1.Initialize(_KRNL);
            _KK2VM = kocKernel2Control1.ViewModel;
            _KK2VM.AfterServerChange += new KocKernel2ViewModel.AfterServerChangeEventHandler(_KK2VM_AfterServerChange);
            _KK2VM.AfterUserChange += new KocKernel2ViewModel.AfterUserChangeEventHandler(_KK2VM_AfterUserChange);
            
            messageLoggingTextBox1.InitViewModel(_KRNL.ML);

            kocMarchControl1.InitializeVM(_KRNL, _KK2VM);

            kocCitySelector1.Initialize(_KRNL);
            kocCitySelector1.ViewModel.ServerNr = _KK2VM.ServerId;
            if (_KK2VM.SelectedUser != null) { kocCitySelector1.ViewModel.UserFbUid = _KK2VM.SelectedUser.FbUid; }
            kocCitySelector1.ViewModel.CitySelected += new KocCitySelectorViewModel.CitySelectedEventHandler(ViewModel_CitySelected);

            kocKnightSelector1.Initialize(_KRNL);
            kocKnightSelector1.ViewModel.ServerNr = _KK2VM.ServerId;
            if (_KK2VM.SelectedUser != null) { kocKnightSelector1.ViewModel.UserFbUid = _KK2VM.SelectedUser.FbUid; }
            kocKnightSelector1.ViewModel.KnightSelected += new KocKnightSelectorViewModel.KnightSelectedEventHandler(ViewModel_KnightSelected);

            _KRNL.LoggingEnabled = true;
            _KRNL.LogPostHandler = true;
            _KRNL.LogTrafficMonitor = true;

        }

        void _KK2VM_AfterUserChange(object sender, UserChangeEventArgs ucea)
        {
            if (_KK2VM.SelectedUser != null)
            {
                kocCitySelector1.ViewModel.UserFbUid = _KK2VM.SelectedUser.FbUid;
                kocKnightSelector1.ViewModel.UserFbUid = _KK2VM.SelectedUser.FbUid;
            }
        }

        void _KK2VM_AfterServerChange(object sender, ServerChangeEventArgs scea)
        {
            kocCitySelector1.ViewModel.ServerNr = _KK2VM.ServerId;
            kocKnightSelector1.ViewModel.ServerNr = _KK2VM.ServerId;
        }

        void ViewModel_CitySelected(object sender, SelectedCityEventArgs scea)
        {
            kocMarchControl1.ViewModel.MHI.CityID = (int)scea.SelectedCity.CityID;
            kocKnightSelector1.ViewModel.SelectedCityId = (int)scea.SelectedCity.CityID;
        }

        void ViewModel_KnightSelected(object sender, SelectedKnightEventArgs skea)
        {
            kocMarchControl1.ViewModel.MHI.KnightID = (int)skea.SelectedKnight.KnightId;
        }
    }
}
